#!/usr/bin/env python
# coding=UTF-8
'''
@Descritpion: 文本处理常用功能函数
@Author: kjoyk
@Since: 2019-08-09 08:32:15
@LastEditor: kjoyk
@LastEditTime: 2019-08-09 08:32:56
'''


def insert_chars_with_position(text, chars='\n\n', position=20, spliter='\n'):
    """文本分段后固定位置插入字符串,初始用于word方格式稿纸排版增加空行

    Arguments:
        text {str} -- 待处理文本

    Keyword Arguments:
        chars {str} -- 插入的字符串 (default: {'\n\n'})
        position {int} -- 插入位置 (default: {20})
        spliter {str} -- 分本分段的字符串 (default: {'\n'})

    Returns:
        [str] -- 结果文本
    
    Tests:
    >>> s='abcd,fed,fsadf,fasd,fasdfasdf'
    
    >>> insert_chars_with_position(s,chars='|',position=2,spliter=',')
    'ab|cd,fe|d,fs|ad|f,fa|sd,fa|sd|fa|sd|f'

    #* 不分段
    >>> insert_chars_with_position(s,chars='|',position=2,spliter=None)
    'ab|cd|,f|ed|,f|sa|df|,f|as|d,|fa|sd|fa|sd|f'
    """

    l = ''
    for p in text.split(spliter):
        _l = list(p)
        for i, j in enumerate(range(position, len(_l), position)):
            _l.insert(i + j, chars)
        if spliter:
            _l.append(spliter)
        l += ''.join(_l)
    return l.strip(spliter)


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=True)
